{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0e87fd8d",
   "metadata": {},
   "source": [
    "# 2.1 数字，字符串和对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e1a5a5de",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'random' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [35]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m wait_time \u001b[38;5;241m=\u001b[39m \u001b[43mrandom\u001b[49m\u001b[38;5;241m.\u001b[39mrandint(\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m60\u001b[39m)\n\u001b[0;32m      2\u001b[0m wait_time\n",
      "\u001b[1;31mNameError\u001b[0m: name 'random' is not defined"
     ]
    }
   ],
   "source": [
    ">>> wait_time = random.randint(1,60)\n",
    ">>> wait_time "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c67e1349",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bottles'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> word = \"bottles\"\n",
    ">>> word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f42dfdb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "odds = [ 1, 3, 5, 7, 9,11,13,15,17,19,\n",
    "        21,23,25,27,29,31,33,35,37,39,\n",
    "        41,43,45,47,49,51,53,55,57,59 ]\n",
    "prices = []\n",
    "temps = [ 32.0,212.0,0.0,81.6,100.0,45.3 ]\n",
    "words = [ 'hello','word' ]\n",
    "car_details = [ 'Toyota','RAV4',2.2,60808 ]\n",
    "everything = [ prices,temps,words,car_details ]\n",
    "odds_and_ends= [ [1,2,3],['a','b','c'],['one','two','three'] ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "21b7f125",
   "metadata": {},
   "outputs": [],
   "source": [
    ">>> vowels = ['a','e','i','o','u']\n",
    ">>> word = \"miliways\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9f183692",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    ">>> for letter in word:\n",
    "            if letter in vowels:\n",
    "                  print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba739a44",
   "metadata": {},
   "source": [
    "# 2.2 在运行时“扩展”列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2ecf7a27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> found = []\n",
    ">>> len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "4b1d61cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> found.append('a')\n",
    ">>> len(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "093ced3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b2bcf382",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> found.append('e')\n",
    ">>> found.append('i')\n",
    ">>> found.append('o')\n",
    ">>> len(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce0e629a",
   "metadata": {},
   "source": [
    "# 2.3 用“in”检查成员关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "77232af0",
   "metadata": {},
   "outputs": [],
   "source": [
    ">>> if 'u'not in found:\n",
    "          found.append('u')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6dd9fb29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', 'o', 'u']"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "95839cfb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:milliways\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "words = input(\"provide a word to search for vowels:\")\n",
    "found = []\n",
    "for letters in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fb38661",
   "metadata": {},
   "source": [
    "### 1.从列表删除对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "c12bed6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> num = [1,2,3,4]\n",
    ">>> num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "14405b11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> num.remove(1)\n",
    ">>> num"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53b1f8cb",
   "metadata": {},
   "source": [
    "### 2.从列表弹出对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "0e7e7ed7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> num.pop()\n",
    ">>> num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "61c4a2ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> num.pop(0)\n",
    ">>> num"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32011093",
   "metadata": {},
   "source": [
    "### 3.用对象扩展列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "b58aef6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 4]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> num.extend([2,4])\n",
    ">>> num"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21275582",
   "metadata": {},
   "source": [
    "### 4.从列表中插入一个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "3ac50720",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 2, 4]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> num.insert(0,1)\n",
    ">>> num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "749f560d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 'two-and-a-half', 2, 4]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> num.insert(2,\"two-and-a-half\")\n",
    ">>> num"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f2a913b",
   "metadata": {},
   "source": [
    "# 2.4 中括号如何使用呢"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "c17a4fa8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "don't panis!\n",
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 's', '!']\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [126]\u001b[0m, in \u001b[0;36m<cell line: 7>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m4\u001b[39m):\n\u001b[0;32m      6\u001b[0m     plist\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;241m0\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m \u001b[43mplist\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m plist\u001b[38;5;241m.\u001b[39mextend([plist\u001b[38;5;241m.\u001b[39mpop(),plist\u001b[38;5;241m.\u001b[39mpop()])\n\u001b[0;32m      9\u001b[0m plist\u001b[38;5;241m.\u001b[39minsert(\u001b[38;5;241m2\u001b[39m,plist\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;241m3\u001b[39m))\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "phrase = \"don't panis!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "118d2645",
   "metadata": {},
   "source": [
    "### 2.4.1列表扩展中括号记法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "a4682481",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> saying = \"don't panic!\"\n",
    ">>> letters = list(saying)\n",
    ">>> letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "835ecd69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d'"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "f0a290aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"'\""
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "963defa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "0478b695",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!'"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "432a89ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'i'"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "3923b028",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[-6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "dd354100",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!'"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> first = letters[0]\n",
    ">>> last = letters[-1]\n",
    ">>> last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "aeb6407c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d'"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> first"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faf85cfc",
   "metadata": {},
   "source": [
    "# 2.5如何复制一个数据结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "572091f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> first = [1,2,3,4,5]\n",
    ">>> first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "f2947579",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> second = first\n",
    ">>> second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "a57948df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> second.append(6)\n",
    ">>> second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "906ec4cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "61591d43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> third = second.copy()\n",
    ">>> third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "56a8fe98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> third.append(7)\n",
    ">>> third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "c7ec2224",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> second"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f257a77",
   "metadata": {},
   "source": [
    "# 2.6 列表认识开始，结束和步长值\n",
    "* 列表切片的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "c68bbc11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# letters[start:stop:step]\n",
    ">>> letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "739eba79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', \"'\", 'p', 'i']"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[0:10:3]\n",
    "#不包括10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "224d7730",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "479b61b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i']"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> letters[:10]\n",
    "#直到10，但不包括10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "ea654b2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', 'n', 't', 'p', 'n', 'c']"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[::2]\n",
    "# 每2个字母选择1个 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da0059b6",
   "metadata": {},
   "source": [
    "* 开始和结束列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "b1b990d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'h',\n",
       " 'i',\n",
       " 't',\n",
       " 'c',\n",
       " 'h',\n",
       " 'h',\n",
       " 'i',\n",
       " 'k',\n",
       " 'e',\n",
       " 'r',\n",
       " \"'\",\n",
       " 's',\n",
       " ' ',\n",
       " 'g',\n",
       " 'u',\n",
       " 'i',\n",
       " 'd',\n",
       " 'e',\n",
       " ' ',\n",
       " 't',\n",
       " 'o',\n",
       " ' ',\n",
       " 't',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'g',\n",
       " 'a',\n",
       " 'l',\n",
       " 'a',\n",
       " 'x',\n",
       " 'y']"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "book = \"The hitchhiker's guide to the galaxy\"\n",
    "booklist = list(book)\n",
    "booklist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "d3f96667",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T', 'h', 'e']"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "booklist[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "f6e1acdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The'"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "2803c32d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'galaxy'"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[-6:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3495bfe",
   "metadata": {},
   "source": [
    "* 列表中使用步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "a9ee9abb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"yxalag eht ot ediug s'rekihhctih ehT\""
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "backwards = booklist[::-1]\n",
    "''.join(backwards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "7072569d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hitchhiker'"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[4:14])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "d6d55646",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'rekihhctih'"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[13:3:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60c3c95b",
   "metadata": {},
   "source": [
    "* 在列表中使用切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "17e69095",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "don't panis!\n",
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 's', '!']\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [133]\u001b[0m, in \u001b[0;36m<cell line: 7>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m4\u001b[39m):\n\u001b[0;32m      6\u001b[0m     plist\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;241m0\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m \u001b[43mplist\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m plist\u001b[38;5;241m.\u001b[39mextend([plist\u001b[38;5;241m.\u001b[39mpop(),plist\u001b[38;5;241m.\u001b[39mpop()])\n\u001b[0;32m      9\u001b[0m plist\u001b[38;5;241m.\u001b[39minsert(\u001b[38;5;241m2\u001b[39m,plist\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;241m3\u001b[39m))\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "phrase = \"don't panis!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "c7341f6d",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "closing parenthesis ')' does not match opening parenthesis '[' (299227747.py, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [134]\u001b[1;36m\u001b[0m\n\u001b[1;33m    new_phrase = new_phrase+''.join([plist[5],plist[4],plist[7],plist[6])\u001b[0m\n\u001b[1;37m                                                                        ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m closing parenthesis ')' does not match opening parenthesis '['\n"
     ]
    }
   ],
   "source": [
    "phrase = \"don't panis!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "new_phrase =''.join(plist[1:3])\n",
    "new_phrase = new_phrase+''.join([plist[5],plist[4],plist[7],plist[6])\n",
    "                                 print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0750f8c3",
   "metadata": {},
   "source": [
    "# 2.7 python的“for”循环了解列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "a7629260",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t m\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n"
     ]
    }
   ],
   "source": [
    "paranoid_android = \"marvin\"\n",
    "letters = list(paranoid_android)\n",
    "for char in letters:\n",
    "    print('\\t',char)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66bcef16",
   "metadata": {},
   "source": [
    "* python的“for”循环了解切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "fece9458",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t m\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "\n",
      "\t\t a\n",
      "\t\t n\n",
      "\t\t d\n",
      "\t\t r\n",
      "\t\t o\n",
      "\t\t i\n",
      "\t\t d\n",
      "\t\t\t a\n",
      "\t\t\t r\n",
      "\t\t\t a\n",
      "\t\t\t n\n",
      "\t\t\t o\n",
      "\t\t\t i\n",
      "\t\t\t d\n",
      "\t\t\t  \n"
     ]
    }
   ],
   "source": [
    "paranoid_android = \"marvin,the paranoid android\"\n",
    "letters = list(paranoid_android)\n",
    "for char in letters[:6]:\n",
    "    print('\\t',char)\n",
    "print()\n",
    "for char in letters[-7:]:\n",
    "    print('\\t'*2,char)\n",
    "for char in letters[12:20]:\n",
    "    print('\\t'*3,char)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10aa418c",
   "metadata": {},
   "source": [
    "* 什么时候不使用列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "5f2a9653",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ford prefect', 'male', 'researcher', 'betelguse seven']"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person1 = ['ford prefect','male','researcher','betelguse seven']\n",
    "person1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "b3045356",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['name',\n",
       " 'ford prefect',\n",
       " 'gender',\n",
       " 'male',\n",
       " 'occupation',\n",
       " 'researcher',\n",
       " 'home planet',\n",
       " 'betegeuse seven']"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person2 = ['name','ford prefect','gender','male','occupation','researcher','home planet','betegeuse seven']\n",
    "person2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8163583d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
